
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Application {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MessagePasser mp = new MessagePasser(args[0], args[1]);

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			try {
				String input = br.readLine();
				String[] inputs = input.split(" ");
				String action = inputs[0];

				if (action.equals("r")) {
					Message m = mp.receive();
					if (m != null)
						System.out.println("receive: " + m);
				} 
				else if (action.equals("s")) {
					if (inputs.length == 4) {
						String name = inputs[1];
						String kind = inputs[2];
						String data = inputs[3];
						Message m = new Message(args[1], name, kind, data);

						mp.send(m);
					}

				} 
				else if (action.equals("m")){
					if(inputs.length == 2)
						mp.multicast(inputs[1]);
				}
				else if(action.equals("i")){
					mp.getClock().increase();
				} 
				else if(action.equals("t")){
					mp.getClock().showTime();
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
